/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.CustomerDAO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.Customer;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
public class AddCustomerAction extends ActionSupport implements ModelDriven<Customer>{
    private Customer customer = new Customer();
    private CustomerDAO authenDAO = new CustomerDAO();
    private List<Customer> customers;
    private Map<Integer, String> sorts;

    public Map<Integer, String> getSorts() {
        return sorts;
    }

    public void setSorts(Map<Integer, String> sorts) {
        this.sorts = sorts;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

     @Action(value = "addcustomer", results = {
        @Result(name = "success", location = "/manageCustomer/w_customer.jsp"),
        @Result(name = "input", location = "/manageCustomer/w_addCustomer.jsp")
    })
    @Override
    public String execute() throws Exception {
        String result;
         //customer = new Customer()   ;
//        Memberaccount memberaccount = new Memberaccount(username, password);
//        customer.setMemeberaccount(memberaccount);
//        aStaff.setLoginName(loginName);
//        aStaff.setPassword(password);
       // System.out.println(aStaff);
        if (authenDAO.InsertCustomer(customer)) {
            addActionMessage("Add staff Successfully!");
            customers = authenDAO.getAllCustomer();
            sorts = new HashMap<Integer, String>();
            sorts.put(1, "First Name");
            sorts.put(2, "Last Name");
            sorts.put(3, "Address");
            sorts.put(4, "DOB");
            result = SUCCESS;
        } else {
            addActionError("Add Failed (due to existed username or email).");
            result = INPUT;
        }

        return result;
    }
    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Customer getModel() {
        return customer; //To change body of generated methods, choose Tools | Templates.
    }
}
